﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using WebAnalyser.UI;

namespace WebAnalyser.Library
{
    class Common 
    {
        // Thank to bb @ http://osix.net/modules/article/?id=586 for the original pattern
        public const string UrlPattern = "^http(s?)://"
                + "?(([0-9a-zA-Z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"     //user@
                + @"(([0-9]{1,3}\.){3}[0-9]{1,3}"                                   // IP- 199.194.52.184
                + "|"                                                               // allows either IP or domain
                + @"(?<tertiaryLevel>([0-9a-zA-Z_!~*'()-]+\.)*)"                    // tertiary domain(s)- www.
                + @"(?<secondLevel>([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.)"  // second level domain
                + "(?<firstLevel>[a-zA-Z]{2,6}))"                                   // first level domain- .com or .museum
                + "(:[0-9]{1,4})?"                                                  // port number- :80
                + "(?<file>((/?)|(/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-/\\s]+)+/?))$";
        public const string DllPattern = @"^\.\\Plugins\\WebAnalyser."
            + @"(?<name>([a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)*)+)"
            + ".dll$";
        /*
         * @todo : faire une gestion plus poussée sur le single
         */
        public static string SingleClassPattern = "[cC][lL][aA][sS]{2,2}\\s*=\\s*(\"|')?"
            + "(?<word>[\\w\\.-]+)"
            + "[^'\"\\s>]";
        public static string MultiClassPattern = "[cC][lL][aA][sS]{2,2}\\s*=\\s*(\"|')"
            + "(?<word>([\\w\\.-]+\\s*)*)"
            + "[^'\"]";
        public const string HrefPattern = "[hH][rR][eE][fF]\\s*=\\s*(\"|')?"
            + @"(?<word>[\w!~*'().;?:@&=+$,%#-/]*)"
            + "[^'\"\\s>]";
        public static int TotalLinks;
        public static int AnalysisStatus;
        public static GroupCollection CurrentUrl;
        public static MainWindow MainWindow;
        public static readonly List<Thread> Threads = new List<Thread>();
        public static bool CancellationPending;
        public static List<string> MatchesToList(MatchCollection matches)
        {
            List<string> resultList = new List<string>();

            for (int i = 0; i < matches.Count; i++)
            {
                resultList.Add(matches[i].Value);
            }
            return resultList;
        }
    }
}
